/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BancoDeDados.Firebird;
 
import BancoDeDados.ConexaoDB;
import Biblioteca.VerificaErros;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.LinkOption;
import java.nio.file.Paths;
import java.sql.DriverManager;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 * ConexaoFirebird
 * 
 * Versao 1.0
 * 
 * Data Criação 28/01/2014
 *
 * @author Igor
 */
public class ConexaoFirebird extends ConexaoDB {
    //Atributos
    private static ConexaoFirebird uniqueInstance;

    //Construtores
    private ConexaoFirebird(){
        this.gerenciarConfiguracoes();

        try {
            //Path path = Paths.get(arquivo);
            super.directoryDB = Paths.get(arquivo);
            super.directoryDB = super.directoryDB.toRealPath(LinkOption.NOFOLLOW_LINKS);
            Class.forName(super.driver);
            super.con = DriverManager.getConnection(super.url + super.directoryDB.toString(),
                    super.userNameDB,
                    super.passwordDB);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, VerificaErros.retornaMensagemErro(e), "Erro!", JOptionPane.ERROR_MESSAGE);
            System.exit(0);
        }
    }
    //Outros Metodos
    @Override
    public void gerenciarConfiguracoes(){
        Properties config = new Properties();
        String arquivoProperties = "./configTREF.properties";
        try {
            config.load(new FileInputStream(arquivoProperties));
        } catch (IOException ex) {
            Logger.getLogger(ConexaoFirebird.class.getName()).log(Level.SEVERE, null, ex);
        }

        String porta = config.getProperty("Firebird_porta");
        String ipServidor = config.getProperty("Firebird_ipServidor");
        String arquivoBD = config.getProperty("Firebird_baconDeDados");
        String userName = config.getProperty("Firebird_userName");
        String password = config.getProperty("Firebird_password");

        this.driver = "org.firebirdsql.jdbc.FBDriver";
        this.url = "jdbc:firebirdsql:"+ipServidor+"/"+porta+":";
        this.userNameDB = userName;
        this.passwordDB = password;
        //O arquivo encontra-se no mesmo diretoWrio da aplicacao 
        this.arquivo = arquivoBD;
    }
    
    public static synchronized ConexaoFirebird getInstance() throws IOException{
        if (uniqueInstance == null) {
            uniqueInstance = new ConexaoFirebird();
        }
        return uniqueInstance;
    }
}
